package cn.demoxy.controller;

import cn.demoxy.annotation.Log;
import cn.demoxy.enums.BusinessTypeEnum;
import cn.demoxy.model.dto.DemoLogDTO;
import cn.demoxy.model.dto.DemoRoleDTO;
import cn.demoxy.model.entity.DemoRole;
import cn.demoxy.model.entity.DemoUser;
import cn.demoxy.result.R;
import cn.demoxy.service.IDemoLogService;
import cn.demoxy.service.IDemoRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @auther demo
 * @date 2022/5/18 20:54
 * 角色表控制层
 */
@Api(tags = "角色接口信息")
@RestController
@RequestMapping("/admin/demo-role")
@AllArgsConstructor
@CrossOrigin
public class DemoRoleController {
    /**
     * 角色service
     */
    private IDemoRoleService roleService;

    /**
     * 根据条件分页查询角色列表
     *
     * @param roleDTO
     * @return
     */
    @ApiOperation(value = "根据条件分页查询角色列表")
    @PostMapping("/page")
    @RequiresPermissions("demo-role:page")
    public R page(@RequestBody DemoRoleDTO roleDTO) {
        return R.ok(roleService.findAllRoleByKeyword(roleDTO));
    }

    /**
     * 添加角色信息
     *
     * @param role
     * @return
     */
    @ApiOperation(value = "添加角色信息")
    @PostMapping("/save")
    @Log(title = "角色管理角色新增",businessType = BusinessTypeEnum.INSERT)
    @RequiresPermissions("demo-role:save")
    public R save(@RequestBody DemoRole role) {
        return R.ok(roleService.save(role));
    }

    /**
     * 编辑角色信息
     *
     * @param role
     * @return
     */
    @ApiOperation(value = "编辑角色信息")
    @PostMapping("/update")
    @Log(title = "角色管理角色信息修改",businessType = BusinessTypeEnum.UPDATE)
    @RequiresPermissions("demo-role:update")
    public R update(@RequestBody DemoRole role) {
        return R.ok(roleService.updateById(role));
    }

    /**
     * 删除角色信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除角色信息")
    @PostMapping("/delete/{id}")
    @Log(title = "角色管理角色删除",businessType = BusinessTypeEnum.DELETE)
    @RequiresPermissions("demo-role:delete")
    public R delete(@PathVariable("id") String id) {
        return R.ok(roleService.deleteRole(id));
    }

    /**
     * 添加权限
     *
     * @param
     * @return
     */
    @ApiOperation(value = "添加权限")
    @PostMapping("/addPermission")
    @Log(title = "角色管理添加权限",businessType = BusinessTypeEnum.INSERT)
    @RequiresPermissions("demo-role:addPermission")
    public R addPermission(@RequestBody DemoRoleDTO roleDTO) {
        return R.ok(roleService.addPermission(roleDTO));
    }

    /**
     * 根据角色id查询菜单
     *
     * @param
     * @return
     */
    @ApiOperation(value = "根据角色id查询菜单")
    @PostMapping("/findMenuByRoleId/{roleId}")
    @RequiresPermissions("demo-role:findMenuByRoleId")
    public R findMenuByRoleId(@PathVariable("roleId") String roleId) {
        return R.ok(roleService.findMenuByRoleId(roleId));
    }
}
